package cz.data.common.support.db.manage;

import cz.data.common.exception.DataException;
import cz.data.common.support.db.DbProperty;
import cz.data.common.support.db.DbType;

import java.util.HashMap;
import java.util.Map;

public final class DbManageRegistry {
    private static final EmptyDbManageFactory defaultFactory = new EmptyDbManageFactory();
    private static final Map<DbType, DbManageFactory> registry = new HashMap<>();

    public static void registry(DbManageFactory factory, DbType... dbTypes) {
        for (DbType dbType : dbTypes) {
            registry.put(dbType, factory);
        }
    }

    public static DbManageFactory factory(DbType dbType) {
        return registry.getOrDefault(dbType, defaultFactory);
    }

    public static class EmptyDbManageFactory implements DbManageFactory {

        @Override
        public DbManage factory(DbProperty property) {
            throw new DataException("数据源不适配该操作");
        }
    }
}
